﻿namespace BM.Tools.WebControls.MForm.Controls.Items
{
    using System.Xml;
    using System.Xml.XPath;

    /// <summary>
    /// The repeatable offspring item that is created from the <see cref="Group" /> definition object.
    /// </summary>
    public class GroupItem : OffspringsOwnerItem
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GroupItem"/> class.
        /// </summary>
        /// <param name="basis">
        /// The basis.
        /// </param>
        /// <param name="index">
        /// The index.
        /// </param>
        /// <param name="navigator">
        /// The navigator.
        /// </param>
        public GroupItem(OffspringsOwner basis, int index, XPathNavigator navigator) : base(basis, index, navigator)
        {
        }

        /// <summary>
        /// Appends the inner xml nodes to the given parent node.
        /// </summary>
        /// <param name="parent">
        /// The parent node. 
        /// </param>
        public override void AppendToParent(XmlNode parent)
        {            
            foreach (IXmlEditor editor in this.GetChildrenXmlEditors())
            {
                if (editor.XmlEditEnabled)
                {
                    editor.AppendToParent(parent);
                }
            }
        }

        /// <summary>
        /// Appends the resulting xml node of this offspring to the given parent node
        /// with the nodes that were left out on xml loading
        /// </summary>
        /// <param name="parent">
        /// The parent node.
        /// </param>
        public override void AppendToParentWithLeftNodes(XmlNode parent)
        {            
            foreach (IXmlEditor editor in this.GetChildrenXmlEditors())
            {
                if (editor.XmlEditEnabled)
                {
                    OffspringItem offspringItem = editor as OffspringItem;
                    if (offspringItem != null)
                    {
                        offspringItem.AppendToParentWithLeftNodes(parent);
                    }
                    else
                    {
                        editor.AppendToParent(parent);
                    }
                }
            }
        }
    }
}
